package com.mes.check.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.stereotype.Repository;

import com.mes.check.dao.iface.IWaixieCheckReportDao;
import com.mes.check.domain.ReworkDetail;
import com.mes.check.domain.WaixieCheckReport;
import com.mes.common.dao.BasicDao;
import com.mes.task.dao.iface.IPartTaskDao;
import com.mes.task.domain.PartTask;

@Repository
public class WaixieCheckReportDao extends BasicDao<WaixieCheckReport> implements IWaixieCheckReportDao{
	private static final Log log = LogFactory.getLog(WaixieCheckReportDao.class);

	@Autowired
	private IPartTaskDao partTaskDao;
	
	@Override
	public WaixieCheckReport getWaixieCheckReport(String partTaskUid, String coDeptId)
			throws Exception {
		WaixieCheckReport result = null;
		List list=null;
		try {
			log.info("++++++++++++++++++++++++++++++++++++getWaixieCheckReportByTaskUid");
			Session session=this.getSession();
			StringBuilder hql=new StringBuilder();
			hql.append(" from " + this.getClass().getName() + " where ");
			hql.append( " partTaskUid=? and coDeptId=?");
			Query query = session.createQuery(hql.toString());
			query.setString(0, partTaskUid);
			query.setString(1, coDeptId);

			list = query.list();
			if(list.size() > 0){
				result = (WaixieCheckReport)list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@Override
	public List<PartTask> getNeedSyncWaixiePartTasksByTime(Date fromDate, Date toDate) throws Exception{
		List<PartTask> list = new ArrayList();
		try{
			List<String> lstTaskUid = null;
			Session session = this.getSession();
			String sql = "select distinct t.parentuid as taskuid from t_tasks t " + 
				"inner join t_assignments a on a.taskuid=t.taskuid and a.assntype=1 " + 
				"inner join m_checktasks ct on ct.taskuid=t.taskuid " +
				"left join m_defectjudgeheader mdj on mdj.taskuid=t.taskuid " +
				"where a.resuid like 'wxcj%' " +  
				"and exists(select 1 from i_ioheader h inner join i_iodetail d on h.invioid=d.invioid " +
				"where h.iotype=106  and d.relationid=t.taskuid) " +
				"and (ct.actualfinish between ? and ? or mdj.judgetime between ? and ?) ";
			Query query = session.createSQLQuery(sql);
			query.setTimestamp(0, fromDate);
			query.setTimestamp(1, toDate);
			query.setTimestamp(2, fromDate);
			query.setTimestamp(3, toDate);
			
			lstTaskUid = query.list();
			for(int i=0; i<lstTaskUid.size(); i++){
				PartTask parttask = partTaskDao.getById(lstTaskUid.get(i));
				list.add(parttask);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return list;
	}
	
}
